/*
 * pcre_wrapper.cpp
 *
 *  Created on: 2012-6-1
 *      Author: li.zhaoping
 */

#include <stdio.h>
#include <string.h>
#include <pcre.h>

int *pcre_new_express(char *pattern) {
	const char *error = NULL;
	int erroffset = -1;
	pcre *re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
	if (re == NULL) {
		printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
		return 0;
	}
	return (int*)re;
}

int pcre_exc_express(int *re, char *data, int dataSize, int offset, int *retStart, int *retLen) {
	int ovector[3];
	int rc = pcre_exec((pcre*)re, NULL, data, dataSize, offset, 0, ovector, 3);
	if (rc >=0){
		*retStart = ovector[0];
		*retLen = ovector[1]-ovector[0];
		return 0;
	}
	return -1;
}

void pcre_free_express(int *re) {
	pcre_free((pcre*)re);
}
